from pyecharts.components import Table
from pyecharts import options as opts
from pyecharts.charts import Line, Bar

# 获取设备信息
import datas_fetch


def company_info(cname, date):
    maxdate = datas_fetch.TimeInfos['datetime'].max()
    if date is None or date == '':
        date = maxdate
    if date > maxdate:
        date = maxdate
    # 公司id
    cid = datas_fetch.CompanyInfos[datas_fetch.CompanyInfos['name'] == cname]['selfid'].to_list()[0]
    # 公司所有设备
    cinstallinfos = datas_fetch.InstallInfos[datas_fetch.InstallInfos['companyid'] == cid]
    # 公司设备对应运行时间
    tinfos = datas_fetch.TimeInfos[datas_fetch.TimeInfos['datetime'] == date]
    # 合并设备信息和设备运行信息
    cinfos = cinstallinfos.join(tinfos.set_index('equipmentnumber'), on='equipmentnumber')
    cinfos.drop(columns=['datetime', 'companyid'], inplace=True)
    cinfos = cinfos.fillna(0)
    cinfos['stoptime'] = round(24 - (cinfos['worktime'] + cinfos['standbytime']), 2)
    return cinfos


# 获取开机的设备信息
def company_start(cinfos):
    cstart = cinfos[(cinfos['worktime'] != 0) & (cinfos['standbytime'] != 0)]
    return cstart


# 生成设备信息表格
def company_table(cinfos, cname, date):
    maxdate = datas_fetch.TimeInfos['datetime'].max()
    if date is None or date == '':
        date = maxdate
    if date > maxdate:
        date = maxdate
    name_list = cinfos['name'].tolist()
    equipMentNumber_list = cinfos['equipmentnumber'].tolist()
    workTime_list = cinfos['worktime'].tolist()
    standbyTime_list = cinfos['standbytime'].tolist()
    stopTime_list = cinfos['stoptime'].tolist()

    headers = ['设备编号', '设备名称', '工作时间(h)', '待机时间(h)', '停机时间(h)']  # '开机率(%)', '运行效率(%)'
    rows = [[equipMentNumber_list[i], name_list[i], workTime_list[i], standbyTime_list[i], stopTime_list[i]] for i in
            range(len(equipMentNumber_list))]
    table = Table()
    table.add(headers=headers, rows=rows, attributes={"class": "etable-click"})
    table.set_global_opts(
        title_opts=opts.ComponentTitleOpts(title="{}-{}".format(cname, date), title_style={'style': 'color:white'}))
    return table


def company_startRate(cinfos):
    c = Line(init_opts=opts.InitOpts(width='100%', height='380px'))
    c.add_xaxis(cinfos.equipmentnumber.tolist())
    c.add_yaxis("", cinfos.startrate.tolist(), color='#33ffff')
    c.set_global_opts(
        title_opts=opts.TitleOpts(title="最新设备开机率(%)", title_textstyle_opts={"color": "white"}),
        datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=60),
        xaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(rotate=-30, color="white"),
            axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="white"))),
        yaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(color="white"),
            axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="white")))
    )
    return c


def company_top(cstart):
    workTimetop = cstart.nlargest(10, 'worktime')
    x_data = list(workTimetop['equipmentnumber'])
    y_data = list(workTimetop['worktime'])
    c = Bar(init_opts=opts.InitOpts(width="100%", height="380px"))
    c.add_xaxis(xaxis_data=x_data)
    c.add_yaxis("", y_axis=y_data, category_gap=50, itemstyle_opts=opts.ItemStyleOpts(color='#33ffff'))
    c.set_global_opts(
        title_opts=opts.TitleOpts("设备工作时长TOP5", title_textstyle_opts={"color": "white"}),
        xaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(rotate=-30, color="white"),
            axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="white"))),
        yaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(color="white"),
            axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="white")))
    )
    return c